﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NIEN_LUAN
{
    public partial class FormChiTietNhaCungCap : Form
    {
        private NIEN_LUAN.Db.DataContentDataContext db = new NIEN_LUAN.Db.DataContentDataContext();
        private FormQuanLyNhaCungCap formQuanlyNCC;
        private FormTimChiTietNCC frmTimKiemChiTietNCC;
        private FormTimKiemNhaCungCap frmTimNhaCungCap;
        private FormLapPhieuNhap frmLapPhieuNhap;
        private Db.NHACUNGCAP NhaCungCapMuonSua;

        public FormChiTietNhaCungCap(FormQuanLyNhaCungCap frm)
        {
            formQuanlyNCC = frm;
            InitializeComponent();
        }

        public FormChiTietNhaCungCap(FormTimChiTietNCC frm)
        {
            this.frmTimKiemChiTietNCC = frm;
            InitializeComponent();        
        }

        public FormChiTietNhaCungCap(FormTimKiemNhaCungCap frm)
        {
            frmTimNhaCungCap = frm;
            InitializeComponent();
        }

        public FormChiTietNhaCungCap(FormLapPhieuNhap frm)
        {
            frmLapPhieuNhap = frm;
            InitializeComponent();
        }

        public FormChiTietNhaCungCap(Db.NHACUNGCAP model, FormQuanLyNhaCungCap frm)
        {            
            InitializeComponent();
            this.txtMaNCC.Text = model.MaNhaCungCap;
            this.txtMaNCC.Enabled = false;
            this.txtTenNCC.Text = model.TenNhaCungCap;
            this.txtSodtNCC.Text = model.SoDienThoai;
            this.txtDiachiNCC.Text = model.DiaChi;
            NhaCungCapMuonSua = model;
            formQuanlyNCC = frm;
        }

        public FormChiTietNhaCungCap(Db.NHACUNGCAP model, FormTimKiemNhaCungCap frm)
        {
            InitializeComponent();
            this.txtMaNCC.Text = model.MaNhaCungCap;
            this.txtMaNCC.Enabled = false;
            this.txtTenNCC.Text = model.TenNhaCungCap;
            this.txtSodtNCC.Text = model.SoDienThoai;
            this.txtDiachiNCC.Text = model.DiaChi;
            NhaCungCapMuonSua = model;
            frmTimNhaCungCap = frm;
        }

        private void btnThoatCTNV_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;//khi click vao day thi tra ve dialog result la cancel
            this.Close();
        }

        private void btnLuuCTNV_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtMaNCC.Text.Trim()))
            {
                MessageBox.Show("Mã NCC không được rỗng","Warning",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }

            if (string.IsNullOrEmpty(this.txtTenNCC.Text.Trim()))
            {
                MessageBox.Show("Tên NCC rỗng", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.IsNullOrEmpty(this.txtDiachiNCC.Text.Trim()))
            {
                MessageBox.Show("Địa chỉ NCC rỗng", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.IsNullOrEmpty(this.txtSodtNCC.Text.Trim()))
            {
                MessageBox.Show("Số điện thoại rỗng", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

             var nhacungcap = new Db.NHACUNGCAP 
            {
                MaNhaCungCap=this.txtMaNCC.Text,                
                TenNhaCungCap=this.txtTenNCC.Text,
                DiaChi=this.txtDiachiNCC.Text,
                SoDienThoai=this.txtSodtNCC.Text
            };           
            
             if (formQuanlyNCC != null && NhaCungCapMuonSua==null)
             {
                 var ncc = (from m in db.NHACUNGCAPs
                           where m.TenNhaCungCap == this.txtTenNCC.Text.Trim()
                           select m).ToList();
                 if (ncc.Count > 0)
                 {
                     MessageBox.Show("Mã nhà cung cấp Bị Trùng!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                     return;
                 }

                 db.NHACUNGCAPs.InsertOnSubmit(nhacungcap);
                 db.SubmitChanges();

                 this.formQuanlyNCC.NhaCungCapAdd = nhacungcap;
                 this.DialogResult = DialogResult.OK;   
             }

             if (frmTimKiemChiTietNCC != null)
             {
                 var ncc = (from m in db.NHACUNGCAPs
                           where m.TenNhaCungCap == this.txtTenNCC.Text.Trim()
                           select m).ToList();
                 if (ncc.Count > 0)
                 {
                     MessageBox.Show("Tên Người Dùng Bị Trùng!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                     return;
                 }

                 db.NHACUNGCAPs.InsertOnSubmit(nhacungcap);
                 db.SubmitChanges();
                 this.frmTimKiemChiTietNCC.nHACUNGCAPBindingSource.Add(nhacungcap);                
                 this.DialogResult = DialogResult.OK;
             }

             if (NhaCungCapMuonSua != null && formQuanlyNCC != null)//sua chi tiet khach hang 
             {
                 ///
                 var _nhacungcap = db.NHACUNGCAPs.Single(m => m.MaNhaCungCap == nhacungcap.MaNhaCungCap);
                 _nhacungcap.SoDienThoai = nhacungcap.SoDienThoai;
                 _nhacungcap.TenNhaCungCap = nhacungcap.TenNhaCungCap;
                 _nhacungcap.DiaChi = nhacungcap.DiaChi;
                 db.SubmitChanges();
                 this.formQuanlyNCC.NhaCungCapSua = _nhacungcap;
                 this.DialogResult = DialogResult.OK;
                 this.Close();
             }

             if (NhaCungCapMuonSua != null && frmTimNhaCungCap != null)//sua chi tiet khach hang 
             {
                 ///
                 var _nhacungcap = db.NHACUNGCAPs.Single(m => m.MaNhaCungCap == nhacungcap.MaNhaCungCap);
                 _nhacungcap.SoDienThoai = nhacungcap.SoDienThoai;
                 _nhacungcap.TenNhaCungCap = nhacungcap.TenNhaCungCap;
                 _nhacungcap.DiaChi = nhacungcap.DiaChi;
                 db.SubmitChanges();
                 this.frmTimNhaCungCap.NhaCungCapSua = _nhacungcap;
                 this.DialogResult = DialogResult.OK;
                 this.Close();
             }

             if (NhaCungCapMuonSua == null && formQuanlyNCC == null && frmLapPhieuNhap != null)
             {
                 var ncc = (from m in db.NHACUNGCAPs
                            where m.TenNhaCungCap == this.txtTenNCC.Text.Trim()
                            select m).ToList();
                 if (ncc.Count > 0)
                 {
                     MessageBox.Show("Mã nhà cung cấp Bị Trùng!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                     return;
                 }

                 db.NHACUNGCAPs.InsertOnSubmit(nhacungcap);
                 db.SubmitChanges();

                 this.frmLapPhieuNhap.NhaCungCapAdd = nhacungcap;
                 //var ncc = (from m in db.NHACUNGCAPs
                 //           select m).ToList;
                 //bsNhaCungCap.DataSource = ncc;
                 
                 this.DialogResult = DialogResult.OK; 
             }
        }
    }
}
